package com.huiminpay.transaction.controller;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class AlipayController {

    private String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    private String appId = "2021000118603153";
    private String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQClQVPWsWftzQB1ZEb09ZBO5aPxJGe3aQkmtgQdp5cvYDhTB3udVFcgaXCZqcd8lgOk4vNwHXpCHaPqn7FZfmCtDMaO1dFOFNk7IA+QODggNF6Zfhw5vPhsX1hRo0Kswxwu6W95JWrZSGvCCkLxnFcVWbnDesHn+eaAE7D/BLjKh/10vPdy2VMg5oV+Ca7vDeKM8t8D6Pol0tK7KUXPmQ3DBozeIpX0t3OnV88ndl8cH2OxU1SB087lIGebjudqKX9pG4gDzi7/lraZ2zwe6XBoZCSXVUmZypeFlKW2fw+/Xk8wlS5r8msPZkyWOm3qoPXP0sr3lPkDspyYJXVWCGg7AgMBAAECggEAOustvxAFY+2BSll0SwX6a4bIisNWDzhtwNWIZiJIbuW5bzDty8w0PCQbt02nCC5cIj+gV8rKuiPjO1WwX54FTe2OJYLqWxJn2QK3Sc4hhKnnSIaD/bn4QHtF++VfckUmGTu1ONY0YD/H26jY8VeyXLRe3Ok5lUGuAuACbQn78Ikxlend3byHtpPW/+XaP2OA2eI6kOpEo1qxSYcRvH2ELdBhh9cn0GswlZyMirQZc3KElYz2+ij2P67aHJ6xoU0b0Vy6E9sO3IhD8KP4yjuf0A5rJdGSOsU+nSPGAKbXyc/tpB6JvTWh88ohkbXqHkzxohF17oh4wtlYzdIOk0P8AQKBgQD/uukGBMWBncNnuSKilC1wXFcXGCSkzr5WmSDmTlHYTDuRGdWc/18yUAEuBm7f6wQgHt2jqH3mQ6d8osEoPk4MlOZHbWk6z8D3TbWOUb50zbWxx3Priebp+PIoK37OEFCFZkpqgVt6aV0K7ESYRtYA9tIXluTbAgzevfIqVYn5AQKBgQClbflT4sFkDHzUUoiVUH47qDy4keVcIxb50ZxxFVjRo7Ts0fz/L6Rd/lcyi+NRcbQ164mlo7eWt0jpF93Ioy7P6B7Gyw8M3U0kPKf/M/QSBh1EYKVv5RQnh757TaM8C5H3783CA0UFAuYpA7JCXZ5M1f0hBrvp6B5ZnP1+hl8FOwKBgEvzLxHiiOYd/NLpYSf36ibcxeDOAtQPsuf1Qpgj2prqySYnDhI6pgNDT5ZjwrouCmOfxyO7/uhMX4ncrJFPL878ygecrwwQd7b7FVjXXkFCdxv9qKfjpPkIPqM/h8Gbhs6XurI3CPwqHsAHu7eeEiDnLS5kWkPRMBFXSHzaOsgBAoGAGufXDFiuAXqydsnxou9TiJplPdOkzSZ73yfDXPUO3BHXJElG764s+oiaYAM2dpGasMZdPc5x1pZ6ykZycOZxf6kOEGEIUpxPppwi6rPld2T9L5jUb21/qKpARcery+FJ3VxoR6zGdfkxu+v4+yhEzdRj9hIlT/bLsOGCrShnne8CgYEA9TRP/TGcVUMJ2Lmuw2D0HHpBY6EIRPkDZRcbWc3hMj+vmoZZp1OZT5PHnvw/MhT2V+rUgScdrZ9GLyCsGE41T8/brixevrlVPBdlsSmVWo0WAidIK0wQ5Ztmd1XGVX+iQRpNgFwjXSLRNwq7TiHksV4S1l8ZsJgAc86PiyxLcc8=";
    private String format = "json";
    private String charset = "utf-8";
    private String aliPayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlw+kjBxTjXd1mOOjYQ2nN1eH+fQ4iOIr76Bop12yoJVSqLRPcR8qCRCaCl3ImlJQH0+aTumG7krfs1SyQM7B73hugTke2Ho8JNY6DusRFrwxJs83ozpAz0HR8l78PkU1Yd32azzdi1nrAqA6V6UjLoILl6FMPhsoAtzUzNRqNvqCkCxLfXbkYgUZeewgAn/O+3YR7AgFdiaCOZrjFDW4dxHvbG8PqsLnJWP+oSi/JAf1ztBMzr0EHB/izIt1N3RpvlfUCFFjx9Zt6ayBORRWZyv06JsXM4Mt+8hOghoS+s+L9IzomEb07UetG84jHApdYoUerpcmkswBVjKy1z3ZZwIDAQAB";
    private String signType = "RSA2";


    @RequestMapping("/aliPayIndex")
    public void aliPayIndex(HttpServletResponse servletResponse) throws AlipayApiException, IOException {


        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format,
                charset, aliPayPublicKey, signType);

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        //request.setBizContent();
        //设置请求参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setSubject("apple");
        model.setOutTradeNo("T"+System.currentTimeMillis());
        model.setTotalAmount("10000");
        //用户支付中途退出返回商户地址
        model.setQuitUrl("---");
        model.setProductCode("QUICK_WAP_PAY");

        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);

        //设置响应体类型，解决乱码问题
        servletResponse.setContentType("text/html;charset="+charset);
        if (response.isSuccess()) {
            System.out.println("调用成功");

            //获取响应体
            String body = response.getBody();
            servletResponse.getWriter().write(body);




        } else {
            System.out.println("调用失败");

            //响应失败信息
            servletResponse.getWriter().write("error!");
        }

        servletResponse.getWriter().close();
        servletResponse.getWriter().flush();
    }



    @RequestMapping("/alipayQuery/{outTradeNo}")
    public void alipayQuery(@PathVariable("outTradeNo") String outTradeNo,HttpServletResponse servletResponse) throws AlipayApiException, IOException {
        AlipayClient alipayClient =
                new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,aliPayPublicKey,signType);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //request.setBizContent();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(outTradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        servletResponse.setContentType("text/html;charset=utf-8");
        if(response.isSuccess()){
            //获取响应体
            String body = response.getBody();
            servletResponse.getWriter().write(body);
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            //响应失败信息
            servletResponse.getWriter().write("error!");
        }

        servletResponse.getWriter().flush();
        servletResponse.getWriter().close();
    }
}
